package org.example.controller;


import net.sf.json.JSONObject;
import org.example.service.NewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * Controller层获取新闻数据
 * </p)
 * @author lczcr
 * @description
 *
 * @data 2022/11/4 9:03
*/
@Controller
@CrossOrigin("*")//所以都允许跨域
public class NewsController {
    @Autowired
    private NewsService newsService;

    @RequestMapping("/getNewsReport")
    @ResponseBody
    public String getNewsReport(){
        Map<String, Object> map = new HashMap<String, Object>();

        //新闻总量
        int newssum = newsService.getNewsNum();
        map.put("newssum",newssum);

        Map<String, Object> newsRank =newsService.getNewsList();
        //新闻浏览量排行
        map.put("name", newsRank.get("name"));
        map.put("newscount", newsRank.get("count"));

        Map<String, Object> periodRank =newsService.getPeriodList();
        //新闻时段浏览量
        map.put("logtime", periodRank.get("logtime"));
        map.put("periodcount", periodRank.get("count"));

        return JSONObject.fromObject(map).toString();
    }

}

